<?php

class Amway_Question extends Model
{
	protected $_table_name = TB_AW_QUESTION;
	protected $_primary_key = 'id';
	
	
	function getQuestionHaveComment($member_id)
	{		
		$sql = "SELECT q.*, c.* FROM ".TB_AW_QUESTION." as q INNER JOIN ".TB_AW_COMMENT." as c ";
		$sql .= " ON q.id = c.question_id  ";
		$sql .= " WHERE c.member_id = ? AND c.active = 1 GROUP BY q.id ORDER BY q.last_update DESC";
		
		$sth = $this->query($sql, array($member_id,));
		
		return $sth->fetchAll(PDO::FETCH_ASSOC);		
	}
	
	function getListDisplay($page_id, $ln_code, $offset = 0, $items_per_page = 0, $order_by = NULL)
	{
		$sql = "SELECT pr.id ,pr.uniqid ,pr.visited ,pr.active ,pr.sort_order ,pr.last_update ,ci.name FROM ".TB_PAGE_CONTENT. " as pr INNER JOIN ".TB_PAGE_CONTENT_LN." as ci ";
		$sql .= " ON pr.id = ci.id ";
		$sql .= " WHERE pr.page_id = ? AND ci.ln = ?";

		if(!empty($order_by))
			$sql .= " ORDER BY {$order_by}";
				
		if ($items_per_page > 0)
			$sql .= " LIMIT {$offset},{$items_per_page}";
		
		$sth = $this->query($sql, array($page_id, $ln_code));
		
		return $sth->fetchAll(PDO::FETCH_ASSOC);
	}
	
	function getRowContent($content_id, $ln_code)
	{
		$sql = "SELECT pr.* ,ci.* FROM ".TB_PAGE_CONTENT. " as pr INNER JOIN ".TB_PAGE_CONTENT_LN." as ci ";
		$sql .= " ON pr.id = ci.id ";
		$sql .= " WHERE pr.id = ? AND ci.ln = ?";
	
		$sth = $this->query($sql, array($content_id, $ln_code));
		$arr =	$sth->fetchAll(PDO::FETCH_ASSOC);
		
		return !empty($arr) ? $arr[0] : NULL;
	}
	
	function getRowDataContent($content_id)
	{
		$rowContent = $this->get($content_id);
		
		$sql = "SELECT ci.* FROM ".TB_PAGE_CONTENT_LN." as ci WHERE ci.id = ?";
	
		$sth = $this->query($sql, array($content_id));
		$arr =	$sth->fetchAll(PDO::FETCH_ASSOC);
		
		$rowLnContent = array();
		foreach ($arr as $row)
		{
			$rowLnContent["{$row['ln']}"] = $row;
		}
		
		$data = array();
		$data['main_field'] = $rowContent;
		$data['ln_field'] = $rowLnContent;
		return $data;
	}	

	function insertContent($page_id, $arrMainField ,$arrLnField)
	{
		$arrMainField['page_id'] = $page_id;
		$arrMainField['uniqid'] = create_uniqid(10);
		$arrMainField['create_at'] = now_to_mysql();
		// TODO : sort_order
		$id = $this->insert($arrMainField);
		
		// Update sort_order
		$data['sort_order'] = $id;
		$this->update($id, $data);
		
		$this->_table_name = TB_PAGE_CONTENT_LN;
		foreach ($arrLnField as $ln => $arrData)
		{
			$arrData['id'] = $id;
			$arrData['ln'] = $ln;
			$arrData['create_at'] = now_to_mysql();
			$this->insert($arrData);
		}
		$this->_table_name = TB_PAGE_CONTENT;
		
		return $id;
	}
	
	function updateContent($content_id, $arrMainField ,$arrLnField)
	{
		// TODO : sort_order
		$this->update($content_id, $arrMainField);
	
		$this->_table_name = TB_PAGE_CONTENT_LN;
		foreach ($arrLnField as $ln => $arrData)
		{
			$this->updateWithCondition("id = {$content_id} AND ln = '{$ln}'",$arrData);
		}
		$this->_table_name = TB_PAGE_CONTENT;
	}
	
	function deleteContent($content_id)
	{
		$sql = "DELETE * FROM ".TB_PAGE_CONTENT_LN;
		$sql .= " WHERE id = ?";
		
		$sth = $this->query($sql, array($content_id));

		return $this->delete($content_id);
	}
	
	
}

?>